/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"fmt"
	"github.com/chanxuehong/wechat/mp/core"
	"github.com/mjiulee/lego"
	_ "github.com/mjiulee/lego"
	_ "github.com/mjiulee/lego/utils"
	"log"
	"saas_sys_base/modules/wechat/service"
	. "saas_sys_base/modules/merchantCard/service"

	//. "lego_wechat/modules/wechat/models"
	//. "lego_wechat/modules/wechat/service"

	_ "encoding/json"
	_ "github.com/chanxuehong/wechat/mp/oauth2"
)

type WechatSdkCardCallbackCtrl struct {
	//service  WechatSdkService
	//appConfigService configService.AppConfigService
	cardTenantRelSvr service.CardTenantRelService
	//wxCardService    service.WxCardService
	//mchCardService    MchCardService
	mchUserMemberCardService MchUserMemberCardService
	mchMemberCardService MchMemberCardService
}

var _wechatSdkCardCallbackCtrl *WechatSdkCardCallbackCtrl

func GetInstanceWechatSdkCardCallbackCtrl() *WechatSdkCardCallbackCtrl {
	return _wechatSdkCardCallbackCtrl
}

func init() {
	vc := new(WechatSdkCardCallbackCtrl)
	_wechatSdkCardCallbackCtrl = vc
	_ctrlList = append(_ctrlList, vc)
}

func (vc *WechatSdkCardCallbackCtrl) CtrlInfo() string {
	return "WechatSdkCardCallbackCtrl"
}

func (vc *WechatSdkCardCallbackCtrl) RoutInit() {
}

func (this *WechatSdkCardCallbackCtrl) CardPassCheckEventHandler(ctx *core.Context) {
	//log.Printf("card_pass_check event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("card_pass_check event")
	appid := string(ctx.QueryParams.Peek("appid"))
	cardid := ctx.MixedMsg.CardId
	// TODO rel := this.cardTenantRelSvr.GetCardTenantRelByWeappIdAndCardId(appid, cardid)
	err := this.mchMemberCardService.CardPassCheckEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		//一个可能的失败原因是，没有对应的卡
		lego.LogError(fmt.Sprintf("CardPassCheckEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}

	//TODO 这里还有一种密文回复，测试的时候注意是否要处理这两种模式 //ctx.AESResponse(resp, 0, "", nil) // aes密文回复
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) CardNotPassCheckEventHandler(ctx *core.Context) {
	//log.Printf("card_not_pass_check event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("card_not_pass_check event")
	appid := string(ctx.QueryParams.Peek("appid"))
	cardid := ctx.MixedMsg.CardId
	err := this.mchMemberCardService.CardNotPassCheckEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("CardNotPassCheckEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) CardGetCardEventHandler(ctx *core.Context) {
	//log.Printf("user_get_card event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("user_get_card event cardcode<%s>", ctx.MixedMsg.UserCardCode)
	appid := string(ctx.QueryParams.Peek("appid")) //ctx.AppId
	cardid := ctx.MixedMsg.CardId

	_, err := this.mchMemberCardService.CardGetCardEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("CardGetCardEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) UserDelCardEventHandler(ctx *core.Context) {
	//log.Printf("user_del_card event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("user_del_card event")
	appid := string(ctx.QueryParams.Peek("appid"))
	cardid := ctx.MixedMsg.CardId
	err := this.mchMemberCardService.UserDelCardEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("UserDelCardEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) UserConsumeCardEventHandler(ctx *core.Context) {
	//log.Printf("user_consume_card event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("user_consume_card event")
	appid := string(ctx.QueryParams.Peek("appid"))
	cardid := ctx.MixedMsg.CardId
	err := this.mchMemberCardService.UserConsumeCardEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("UserConsumeCardEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) UserViewCardEventHandler(ctx *core.Context) {
	//log.Printf("user_view_card event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("user_view_card event cardcode<%s>", ctx.MixedMsg.UserCardCode)
	appid := string(ctx.QueryParams.Peek("appid"))

	cardid := ctx.MixedMsg.CardId
	err := this.mchMemberCardService.UserViewCardEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("UserViewCardEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) UserEnterSessionEventHandler(ctx *core.Context) {
	//log.Printf("user_enter_session_from_card event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("user_enter_session_from_card event")
	appid := string(ctx.QueryParams.Peek("appid"))
	cardid := ctx.MixedMsg.CardId
	err := this.mchMemberCardService.UserEnterSessionEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("UserEnterSessionEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}

func (this *WechatSdkCardCallbackCtrl) SubmitMemberCardUserInfoEventHandler(ctx *core.Context) {
	//log.Printf("submit_membercard_user_info event:\n%s\n", ctx.MsgPlaintext)
	log.Printf("submit_membercard_user_info event")
	appid := string(ctx.QueryParams.Peek("appid"))
	cardid := ctx.MixedMsg.CardId
	err := this.mchMemberCardService.SubmitMemberCardUserInfoEventHandler(appid, ctx.MixedMsg)
	if err != nil {
		lego.LogError(fmt.Sprintf("SubmitMemberCardUserInfoEventHandler Failed: appid<%s>, cardid<%s>", appid, cardid))
	}
	ctx.NoneResponse()
}
